package com.cysyz.web.action

import org.apache.struts.actions.DispatchAction
import org.apache.struts.action.ActionMapping
import org.apache.struts.action.ActionForward
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.apache.struts.action.ActionForm
import org.springframework.stereotype.Controller
import com.cysyz.entity.product.Brand
import com.cysyz.web.form.BrandForm
import com.cysyz.service.product.impl.BrandService
import javax.annotation.Resource
import com.cysyz.dao.impl.InvalidIdException
import com.cysyz.dao.BaseDao
import java.util.Arrays

@Controller("/contorl/brand/manage")
class BrandManageAction extends DispatchAction{
	@Resource BrandService brandService
	@Resource BaseDao dao
	def public ActionForward addUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		mapping.findForward("add");
	}

	def public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		println(request.requestURL)
		println(request.requestURI)
		var brandForm = form as BrandForm
		var Brand brand = null; 
		try {
			brand = dao.get(typeof(Brand), brandForm.brandid)
		} catch(InvalidIdException e) {
			e.printStackTrace			
		}
		request.setAttribute("brand",brand);
		mapping.findForward("edit");
	}

	def public ActionForward edit (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		var brandForm = form as BrandForm
		var brand = new Brand()
		brand.name = brandForm.name
		brand.brandid = brandForm.brandid
		if (brandForm.logoFile != null && brandForm.logoFile.fileSize > 0) {
			println(brandForm.logoFile.fileName)
			var updateloadableExt = Arrays.asList("gif", "jpg", "png", "bmp")
			var arrowType = Arrays.asList("image/bmp","image/png","image/gif","image/jpg","image/jpeg","image/pjpeg")
			var fileName = brandForm.logoFile.fileName
			val ext = fileName.substring(fileName.lastIndexOf(".") + 1)
			if (updateloadableExt.contains(ext.toLowerCase) && arrowType.contains(brandForm.logoFile.contentType.toLowerCase)) {
				brandService.edit(brand, brandForm.logoFile.fileName, brandForm.logoFile.fileData, request.getRealPath("/"))
			} else {
				println("===========error============")
			}
		} else {
			brandService.edit(brand)
		}
		new ActionForward("/contorl/brand/list.do?page=1")
	} 
	
	def public ActionForward queryUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		mapping.findForward("query");
	}
	
	def public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {	
		
		var brandForm = form as BrandForm
		var brand = new Brand()
		brand.name = brandForm.name
		if (brandForm.logoFile != null && brandForm.logoFile.fileSize > 0) {
			println(brandForm.logoFile.fileName)
			brandService.add(brand, brandForm.logoFile.fileName, brandForm.logoFile.fileData, request.getRealPath("/"))
		} else {
			brandService.add(brand)	
		}
		mapping.findForward("list")
	}
}